<!DOCTYPE html>
<html class="bg-white" xmlns:th="http://www.thymeleaf.org">
<head th:include="include :: header"></head>
<link rel="stylesheet" th:href="@{/css/admin.css}" media="all" />
<body>
<form id="deptForm" lay-filter="deptForm" class="layui-form model-form">
    <input id="treeId" name="parentId"  type="hidden"/> <!--上级部门Id-->
    <div class="layui-form-item">
        <label class="layui-form-label">上级部门</label>
        <div class="layui-input-block">
            <input name="parentName" id="treeName" placeholder="" type="text" class="layui-input inputsur" maxlength="20"  readonly="readonly" lay-verify="required" required/>
        </div>
    </div>
    <input name="deptId" type="hidden"/> <!--部门Id-->
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input name="deptName" placeholder="请输入部门名称" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示顺序</label>
        <div class="layui-input-block">
            <input name="orderNum" placeholder="请输入显示顺序" type="text" class="layui-input" lay-verify=""
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-block">
            <input name="leader" placeholder="请输入显示顺序" type="text" class="layui-input" lay-verify=""
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">联系电话</label>
        <div class="layui-input-block">
            <input name="phone" placeholder="请输入显示顺序" type="text" class="layui-input" lay-verify=""
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input name="email" placeholder="请输入显示顺序" type="text" class="layui-input" lay-verify=""
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="启用" checked/>
            <input type="radio" name="status" value="1" title="停用"/>
        </div>
    </div>
    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="btnSubmit" lay-submit>保存</button>
    </div>
</form>

<!-- js部分 -->
<div th:include="include::footer"></div>
<script type="text/javascript" th:src="@{/js/common.js}"></script>

<script>
    layui.use(['layer', 'form','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;

        var url = ctx+"system/dept/add";
        // 回显数据
        var dept = admin.getTempData('t_dept');
        if(dept){   //如果是修改数据
            form.val('deptForm', post);
            url = ctx+"system/dept/edit";
        }
        else{   //新增数据
            var checkNodeInfo =admin.getTempData('t_checkNode');
            $("input[name='parentId']").val(checkNodeInfo.deptId); //上级部门Id
            $("input[name='parentName']").val(checkNodeInfo.deptName); //上级部门名称

        }
        //监听指定开关
   /*     form.on('switch(switchTest)', function (data) {
            if (this.checked) {
                $("#status").val(0); //启用
            }
            else {
                $("#status").val(1);//停用
            }
        });*/

        // 表单提交事件
        form.on('submit(btnSubmit)', function (data) {
            layer.load(2);
            console.log(url);
            $.post(url, data.field, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    top.layer.msg(data.msg, {icon: 1});
                    admin.putTempData('formOk', true);  // 操作成功刷新表格
                    // 关闭当前iframe弹出层
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                } else {
                    top.layer.msg(data.msg, {icon: 2});
                }
            });
            return false;
        });


        //部门管理-新增-选择父部门树
        $("input[name='parentName']").click(function () {
            showSelectTree();
        });

        //跳转到部门树页面
        function showSelectTree() {
            layer.open({
                type: 2,
                title: '部门选择',
                area: ['380px', '455px'],
                content: ctx+'system/dept/selectDeptTree/'+ $("#treeId").val(),
                btn: ['确定','关闭'],
                yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    $("#treeId").val(body.find('#treeId').val());
                    $("#treeName").val(body.find('#treeName').val());
                    layer.close(index);
                }, cancel: function () {
                    return true;
                }
            });
        }

    });
</script>
</body>
</html>